<script setup>
import { ref } from "vue";

// 获取当前日期并格式化为 ['YYYY', 'MM', 'DD'] 格式
const now = new Date();
const currentDate = ref([
  now.getFullYear().toString(),
  (now.getMonth() + 1).toString().padStart(2, '0'),
  now.getDate().toString().padStart(2, '0')
]);

// 将 time 默认设置为当前日期的字符串格式
const time = ref(`${now.getFullYear()}-${(now.getMonth() + 1).toString().padStart(2, '0')}-${now.getDate().toString().padStart(2, '0')}`);
const unit = ref('');
const remarks = ref('');
const document = ref('');

const list = ref([
  {
    model: time,
    name: '入库时间',
    label: '入库时间',
    placeholder: '请选择入库时间'
  },
  {
    model: unit,
    name: '来货单位',
    label: '来货单位',
    placeholder: '请输入来货单位'
  },
  {
    model: remarks,
    name: '备注信息',
    label: '备注信息',
    placeholder: '请输入备注信息'
  },
  {
    model: document,
    name: '业务单据',
    label: '业务单据',
    placeholder: '拍照上传'
  },
]);

const showPicker = ref(false);
const minDate = ref(new Date()); // 确保 minDate 是 Date 对象

const isClickFn = () => {
  showPicker.value = true;
};

const onConfirm = (selectedValue) => {
  time.value = selectedValue.selectedValues.join('-');
  showPicker.value = false;
};

// 设置初始日期
minDate.value = new Date();
</script>

<template>
  <div class="marginTop roundCorner py-[10px] box-border backgroundColor">
    <van-form>
      <van-field
          v-for="(item, index) in list"
          :is-link="item.name === '入库时间'"
          :key="index"
          :required="item.name === '入库时间'"
          v-model="item.model"
          :name="item.name"
          :label="item.label"
          :placeholder="item.placeholder"
          :readonly="item.name === '入库时间'"
          @click="item.name === '入库时间' ? isClickFn() : null"
          :type="item.name === '备注信息'? 'textarea': 'text'"
          :right-icon="item.name === '业务单据'? 'photograph': null"
      />
    </van-form>
  </div>
  <van-popup v-model:show="showPicker" position="bottom">
    <van-date-picker
        v-model="currentDate"
        title="选择年月日"
        :min-date="minDate"
        @confirm="onConfirm"
        @cancel="showPicker = false"
    />
  </van-popup>
</template>

<style scoped>
</style>